package com.hjy.product.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hjy.common.utils.PageUtils;
import com.hjy.common.utils.Query;
import com.hjy.product.dao.ProductAttrValueDao;
import com.hjy.product.dao.SkuImagesDao;
import com.hjy.product.dao.SkuInfoDao;
import com.hjy.product.dao.SkuSaleAttrValueDao;
import com.hjy.product.entity.SkuImagesEntity;
import com.hjy.product.entity.SpuInfoDescEntity;
import com.hjy.product.service.SkuImagesService;
import com.hjy.product.service.SkuInfoService;
import com.hjy.product.service.SkuSaleAttrValueService;
import com.hjy.product.service.SpuInfoDescService;
import com.hjy.product.vo.item.SkuItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hjy.product.entity.SkuInfoEntity;
import org.springframework.transaction.annotation.Transactional;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private ProductAttrValueDao productAttrValueDao;

    @Autowired
    private SkuSaleAttrValueDao skuSaleAttrValueDao;

    @Autowired
    private ExecutorService executorService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        /*
        key: '华为',//检索关键字
        catelogId: 0,
        brandId: 0,
        min: 0,
        max: 0
         */
        String key = (String) params.get("key");
        if (StrUtil.isNotBlank(key)) {
            wrapper.and(skuInfoEntityQueryWrapper -> {
                skuInfoEntityQueryWrapper.eq("sku_id", key).or().like("sku_name", key);
            });
        }
        String catelogId = (String) params.get("catelogId");
        if (StrUtil.isNotBlank(catelogId) && Integer.parseInt(catelogId) != 0) {
            wrapper.eq("catalog_id", catelogId);
        }
        String brandId = (String) params.get("brandId");
        if (StrUtil.isNotBlank(brandId) && Integer.parseInt(brandId) != 0) {
            wrapper.eq("brand_id", brandId);
        }
        String min = (String) params.get("min");
        if (StrUtil.isNotBlank(min) && Integer.parseInt(min) > 0) {
            wrapper.ge("price", min);
        }
        String max = (String) params.get("max");
        if (StrUtil.isNotBlank(max) && Integer.parseInt(max) > 0) {
            wrapper.le("price", max);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 查询详情信息
     * @param skuId
     * @return
     */
    @Override
    public SkuItemVo item(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();

        // 第一个异步任务需要首先完成, 其提供了spuId和catalogId
        CompletableFuture<SkuInfoEntity> skuInfoEntityCompletableFuture = CompletableFuture.supplyAsync(() -> {
            // 1, 获取 sku 基本信息 pms_sku_info
            SkuInfoEntity skuInfo = this.getById(skuId);
            skuItemVo.setSkuInfo(skuInfo);
            return skuInfo;
        }, executorService);

        SkuInfoEntity skuInfoEntity = skuInfoEntityCompletableFuture.join();
        Long spuId = skuInfoEntity.getSpuId();
        Long catalogId = skuInfoEntity.getCatalogId();

        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            // 2, 获取 sku 图片信息 pms_sku_images
            List<SkuImagesEntity> skuImagesEntityList = skuImagesService.findImagesBySkuId(skuId);
            skuItemVo.setSkuImages(skuImagesEntityList);
        }, executorService);

        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            // 3, 获取 spu 销售属性的集合
            List<SkuItemVo.SpuItemSaleAttr> spuItemSaleAttrs = skuSaleAttrValueDao.findBySpuId(spuId);
            skuItemVo.setSpuItemSaleAttrs(spuItemSaleAttrs);
        }, executorService);

        CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> {
            // 4, 获取 spu 介绍
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.findBySpuId(spuId);
            skuItemVo.setSpuInfoDesc(spuInfoDesc);
        }, executorService);
        CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> {
            // 5, 获取对应 spu 规格属性的集合组
            List<SkuItemVo.SpuItemBaseAttrGroup> spuItemBaseAttrGroups = productAttrValueDao.findByCatalogIdAndSpuId(catalogId,spuId);
            skuItemVo.setSpuItemBaseAttrGroups(spuItemBaseAttrGroups);
        }, executorService);

        // 等待所有异步任务完成
        CompletableFuture.allOf(future1,future2,future3,future4).join();
        return skuItemVo;
    }

}